package com.yanze.customviewdemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class BeaconMark extends View {
    private Paint mCirclePaint, mFillPaint, mTxtPaint;
    private float mRadius = 30;
    private static final int SIZE_TXT = 24;

    private static final int COLOR_SELECT = Color.parseColor("#FF8D36");
    private static final int COLOR_NORMAL = Color.parseColor("#00A854");
    private static final int COLOR_ERROR = Color.parseColor("#FFDE00");//FFDE00  FF0000

    public BeaconMark(Context context) {
        this(context, null);
    }

    public BeaconMark(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BeaconMark(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mFillPaint = new Paint();
        mFillPaint.setColor(COLOR_NORMAL);
        mFillPaint.setStyle(Paint.Style.FILL);

        mCirclePaint = new Paint();
        mCirclePaint.setColor(Color.WHITE);
        mCirclePaint.setStrokeWidth(3);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.STROKE);

        mTxtPaint = new Paint();
        mTxtPaint.setColor(Color.BLACK);
        mTxtPaint.setTextSize(SIZE_TXT);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = (int) (mRadius * 2) + (SIZE_TXT * 2);
        String minorStr = minor + "";
        int width = Math.max((int) (mRadius * 2), (SIZE_TXT * minorStr.length()));
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.GRAY);

        canvas.save();

        canvas.translate(getWidth() / 2, getHeight() / 2);

        canvas.drawCircle(0, 0, mRadius - 3, mFillPaint);
        canvas.drawCircle(0, 0, mRadius - 3, mCirclePaint);

        int rssiW = rssi.trim().length() * SIZE_TXT;
        canvas.drawText(rssi, (-rssiW / 4), -mRadius, mTxtPaint);

        int minorW = minor.trim().length() * SIZE_TXT;
        canvas.drawText(minor, (-minorW / 4), mRadius * 2, mTxtPaint);

//        canvas.restore();
    }

    static final String minor = "480886";
    static final String rssi = "-90";
}
